#!/bin/bash
# AUTHOR: Vovchik - 14 May 2009
# PURPOSE: GUI to show IP config info
#120201 BK: internationalized.
#130216 mavrothal: < > mess up gtkdialog.
#131126 zigbert: gui (gtkdialog) improvements.

set -a
export TEXTDOMAIN=ipinfo
export OUTPUT_CHARSET=UTF-8

[ -f $HOME/.ipinfo ] && . $HOME/.ipinfo || CB0=false

# --------------
# functions
# --------------

get_ip()
{
	# external ip
	if [ "$CB0" = "true" ];then
	 var0='unresolved'
	 # see http://unix.stackexchange.com/questions/22615/how-can-i-get-my-external-ip-address-in-a-shell-script
	 read -r a b c d <<<$(busybox nslookup myip.opendns.com resolver1.opendns.com|tail -n1)
	 [ -z "$c" ] || var0=$c
	 # rough test for valid ip
	 busybox ipcalc -bs $var0 || var0='unresolved'
	else
	 var0="autocheck disabled"
	fi
	var0="$(gettext '<b>External IP</b> (via opendns.com):') ""<b>$var0</b>"
}

#---------------

info_func() # external ip info
{
	$DEFAULTBROWSER https://www.opendns.com/ &
}

# --------------

get_data()
{
	nl='
'
	# tab 1 - interfaces
	var01=`echo $(gettext 'Hostname:') "$HOSTNAME"`
	var02="`ifconfig | tr '>' ')' | tr '<' '('`" #130216 mavrothal: < > mess up gtkdialog.
	# tab 3 - routing
	var03="`route -n | sed -e 's%Kernel IP routing table%%'`"
	# tab 2
	var04="DNS:"
	var05=`cat /etc/resolv.conf`
	# tab 4 - collect wireless data
	wifi="`iwgetid -a | gawk '{print $1}'`"
	# echo "mywifi: ""$wifi"
	spaces="     "
	if [ -n "$wifi" ]; then 
		var06=`iwgetid -a 2>&1`
		var07=`iwgetid -c 2>&1`
		var08=`iwgetid -f 2>&1`
		var09=`iwgetid -m 2>&1`
		var10=`iwlist encryption 2>&1 | grep -v "no encryption"`
		var11=`iwlist rate 2>&1 | grep Current`
		# concatenate wireless data for display
		var12="$var06""$nl""$var07""$nl""$var08""$nl""$var09""$nl""$var10""$nl""$var11""$nl"
		var12="`echo "$var12" | tr '>' ')' | tr '<' '(' | sed '/^$/d'`" #130216 mavrothal: < > mess up gtkdialog.
	else
		var12="<b>$(gettext 'No wireless extensions detected.')</b>"
	fi
	# tab 5 - collect connection data
	var13="`netstat -tan`"
}	

# --------------

show_data()
{
	echo "$var01"
	echo
	echo "$var02"
	echo
	echo "$var03"
	echo
	echo "$var04"
	echo "$var05"
	echo
	echo "$var12"
	echo "$var13"
}

# --------------

make_gui()
{
XML0='<vbox scrollable="true" shadow-type="0" border-width="15">
       <hbox space-expand="true" space-fill="true">
        <checkbox>
         <label>'$(gettext 'Query opendns.com for your external IP ')'</label>
         <variable>CB0</variable>
         <default>'$CB0'</default>
        </checkbox>
        <button tooltip-text="'$(gettext 'Get information about opendns.com')'" space-expand="true" space-fill="false">
         <input file stock="gtk-info"></input>
         <action>info_func</action>
        </button>
       </hbox>
      <hbox>
         <text use-markup="true" wrap="false" space-expand="false" space-fill="false">
           <label>"<tt>'

XML1='<vbox scrollable="true" shadow-type="0" border-width="15">
       <hbox>
         <text use-markup="true" wrap="false" space-expand="false" space-fill="false">
           <label>"<tt>'
XML2='    </tt>"</label>
         </text>
         <text space-expand="true" space-fill="true"><label>""</label></text>
       </hbox>
       <text space-expand="true" space-fill="true"><label>""</label></text>
     </vbox>'

export IP_Info='
<window title="IP Info - '$(gettext 'Network information')'" icon-name="gtk-network" default_height="450" default_width="600">
<vbox space-expand="true" space-fill="true">
  <notebook labels="'$(gettext 'Interfaces')'|'$(gettext 'DNS')'|'$(gettext 'Routing')'|'$(gettext 'Wireless')'|'$(gettext 'Statistics')'" space-expand="true" space-fill="true">
    '$XML0'<b>'${var01}'</b>'${nl}${var0}${nl}${nl}${var02}''$XML2'
    '$XML1'<b>'${var04}${nl}'</b>'${nl}${var05}''$XML2'
    '$XML1'<b>'$(gettext 'Kernel IP routing table')'</b>'${nl}${var03}${nl}${nl}''$XML2'
    '$XML1'<b>'$(gettext 'Wireless configuration:')${nl}'</b>'${nl}${var12}''$XML2'
    '$XML1'<b>'$(gettext 'Network statistics:')'</b>'${nl}${nl}${var13}''$XML2'
  </notebook>
  <hbox space-expand="false" space-fill="false">
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Ok")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon ok`"'
      <action>exit:OK</action>
    </button>
  </hbox>
</vbox>
</window>'
}

# --------------

display_gui()
{
	eval "`gtkdialog -p IP_Info`"
}

# --------------
# main
# --------------

/usr/lib/gtkdialog/box_splash -close never -text "$(gettext 'Please wait, probing...')" &
XPID=$!
get_ip
get_data
make_gui
kill $XPID

if [ $DISPLAY ] ; then
	display_gui
else
	show_data
fi

echo "CB0=$CB0" > $HOME/.ipinfo
exit 0

# --------------
# end main
# --------------
